#!/bin/bash
clear

# INFO='\033[97;44m'
# WARNING='\033[97;43m'
# SUCCESS='\033[97;42m'
# GREEN='\033[92m'
# YELLOW='\033[93m'
TEXT='\033[30;107;2m'
ERROR='\033[97;41m'
RESET='\033[0m'

if [ "${EUID}" -ne 0 ]; then
  echo -e "${ERROR} ERROR ${RESET}${TEXT} Anda tiada kebenaran untuk menjalankan skrip ini! ${RESET}"
  exit 1
fi

ipAddress=$(wget -qO- ipv4.icanhazip.com)
serverPort=$(grep -E "listen_port" "/etc/wireguard/wg0.conf" | cut -d = -f "2" | tr -d "'") # listen_port='51820'
endPoint="${ipAddress}:${serverPort}"

until [[ ${ClientName} =~ ^[a-zA-Z0-9_-]+$ && ${checkClientName} == '0' && ${#ClientName} -lt 16 ]]; do
  read -rp "Masukkan nama pengguna: " -e ClientName
  checkClientName=$(grep -c -E "^### START ${ClientName}\$" "/etc/wireguard/wg0.conf")

  if [[ ${checkClientName} == '1' ]]; then
    echo
    echo "Client name was already created, please choose another name."
    echo
  fi
done
read -p "Masukkan tempoh aktif [Hari]: " DurationTime
expiredDate=$(date -d "$DurationTime days" +"%F")

for clientEndDotIP in {2..254}; do
  checkEndDotIP=$(grep -c "${ipAddress::-1}${clientEndDotIP}" "/etc/wireguard/wg0.conf")
  if [[ ${checkEndDotIP} == '0' ]]; then
    break
  else
    echo "The subnet configured supports only 253 clients."
    exit 1
  fi
done

until [[ ${IPV4_EXISTS} == '0' ]]; do
  read -rp "Client's WireGuard IPv4: ${ipAddress::-1}" -e -i "${clientEndDotIP}" clientEndDotIP
  clientIPAddress="${ipAddress::-1}${clientEndDotIP}"
  IPV4_EXISTS=$(grep -c "$clientIPAddress" "/etc/wireguard/wg0.conf")

  if [[ ${IPV4_EXISTS} == '1' ]]; then
    echo
    echo "Ip address was already used, please choose another ip address."
    echo
  fi
done

wg genkey | tee /etc/wireguard/clients/$ClientName/privatekey |
  wg pubkey | tee /etc/wireguard/clients/$ClientName/publickey
wg genpsk | tee /etc/wireguard/clients/$ClientName/presharedkey

clientPrivateKey=$(cat /etc/wireguard/clients/$ClientName/privatekey)
clientPublicKey=$(cat /etc/wireguard/clients/$ClientName/publickey)
clientPresharedKey=$(cat /etc/wireguard/clients/$ClientName/presharedkey)
domainNameServer='8.8.8.8,8.8.4.4'
serverPublicKey=$(cat /etc/wireguard/publickey)
domainName=$(cat /usr/local/.environment | grep 'DOMAIN' | cut -d '=' -f 2)

echo "$ClientName $DurationTime $expiredDate" >>/etc/wireguard/clients/.accounts

echo "[Interface]
Address = $clientIPAddress/32
DNS = $domainNameServer
PrivateKey = $clientPrivateKey

[Peer]
AllowedIPs = 0.0.0.0/0
PublicKey = $serverPublicKey
PresharedKey = $clientPresharedKey
Endpoint = $endPoint" >>/etc/wireguard/clients/$ClientName/$ClientName.conf

echo "### START $ClientName $expiredDate
[Peer]
PublicKey = $clientPublicKey
PresharedKey = $clientPresharedKey
AllowedIPs = $clientIPAddress/32
### END $ClientName $expiredDate" >>/etc/wireguard/wg0.conf

systemctl restart wg-quick@wg0

qrencode -t ansiutf8 \
  -o "/etc/wireguard/clients/$ClientName/$ClientName.png" \
  -r "/etc/wireguard/clients/$ClientName/$ClientName.conf"
client_qrcode=$(cat /etc/wireguard/clients/$ClientName/$ClientName.png)

clear
echo
echo -e "${TEXT} ========================================================== ${RESET}"
echo -e "${TEXT} WIREGUARD - BUAT AKAUN PENGGUNA                            ${RESET}"
echo -e "${TEXT} ---------------------------------------------------------- ${RESET}"
echo
echo " Alamat IP     : $ipAddress"
echo " Nama domain   : $domainName"
echo " Nama pengguna : $ClientName"
echo " Tempoh aktif  : $DurationTime hari"
echo " Tarikh luput  : $expiredDate"
echo
echo "$client_qrcode"
echo
echo -e "${TEXT} ---------------------------------------------------------- ${RESET}"
echo -e "${TEXT} Dicipta oleh Doctype, Dikuasakan oleh Cybertize.           ${RESET}"
echo -e "${TEXT} ========================================================== ${RESET}"
echo
